var rootPath = getRootPath();

$(function() {
	var state = $("#state").val();
	
	$('#verifyTable').DataTable({
        "language": {
        	"lengthMenu": "每页 _MENU_ 条记录",
            "zeroRecords": " ",
            "info": "当前 _START_ 条到 _END_ 条 &nbsp;&nbsp;&nbsp;共 _TOTAL_ 条记录",
            "infoEmpty": "无记录",
            "infoFiltered": "",//(从 _MAX_ 条记录过滤)
            "search": "搜索",
            "processing": "载入中",
            "paginate": {
                "first": "首页",
                "previous": "上一页",
                "next": "下一页",
                "last": "尾页"
            }
        },

        serverSide: true,
        info: true,
        lengthChange: false,//是否允许用户改变表格每页显示的记录数
        ordering: false,//是否允许用户排序
        paging: true,//是否分页
        processing: true,
        searching: true,
        pagingType: "simple_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
        stateSave: true,//刷新时是否保存状态
        autoWidth: true,//自动计算宽度
        orderMulti: true,
        pageLength: 10,
        fnInitComplete:function(){//创建完成回调缩略图创建
        	//$('.galpop').galpop();
        },
		"ajax" : {
			"type": "POST",
			"url": rootPath + '/verify/queryVerifiesByDataTable',
			"dataType":'json',
			"contentType": "application/json",
		    "data": function ( data ) {
		    	var rleId = $("option[name='rleId']:selected").val();
				var state = $("#state").val();
				var where = $("#where").val();
				
		    	if(state && $.trim(state)!=""){
		    		data.columns[4].search.value = $.trim(state);
		    	}else{
		    		data.columns[4].search.value = "";
		    	}
		    	
		    	if(rleId && $.trim(rleId)!=""){
		    		data.columns[1].search.value = $.trim(rleId);
		    	}else{
		    		data.columns[1].search.value = "";
		    	}
		    	
		    	if(where && $.trim(where)!=""){
		    		data.columns[3].search.value = $.trim(where);
		    	}else{
		    		data.columns[3].search.value = "";
		    	}
		    	
		    	
		    	return JSON.stringify( data );
		    }
		},
		"columns" : [{
			"name" : "uid",
			"data" : "uid",
			"orderable" : false
		},{
			"name" : "rleId",
			"data" : "rleId"
		},{
			"name" : "realName",
			"data" : null,
			"render": function (data,type,row,meta) {
				return data.realNameTmp.realName;
			},
			"orderable" : false
		},
		{
			"name" : "rleName",
			"data" : null,
			"render": function (data,type,row,meta) {
				return data.role.rleName;
			},
			"orderable" : false,
			"searchable" : false
		},
		{
			"name" : "state",
			"data" : null,
			"render": function (data,type,row,meta) {
				return formatState(data.state);
			},
			"orderable" : false,
			"searchable" : false
		},
		{
			"name" : "cts",
			"data" : null,
			"render": function (data,type,row,meta) {
				return data.cts;
			},
			"orderable" : false,
			"searchable" : false
		},
		{
			"name" : "autoCheckResult",
			"data" : null,
			"render": function (data,type,row,meta) {
				return formatInfo(data.autoCheckResult);
			},
			"orderable" : false,
			"searchable" : false
		},{
			"name":null,
			"data":null,
			"render": function (data,type,row,meta) {
				var rlt = "<a title='编辑' href='javascript:;' onclick='verify_add_edit(\"编辑认证\"," + "\"verifyEditPage?uid=" + data.realNameTmp.uid + "&rleId=" + data.role.rleId + "\", \"\",\"\")' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe60a;</i></a>";
				rlt += "<a title='删除' href='javascript:;' onclick='verify_del(this," + "\"" + data.realNameTmp.uid + "\"," + "\"" + data.role.rleId + "\")' class='ml-5' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe612;</i></a>";
				return rlt;
			},
			"orderable" : false,
			"searchable" : false
		}],
	});
	//身份下拉选框
	$.ajax({
		url: rootPath + '/role/queryRolesByVerifyAndDr',
        type: 'GET',
        data : {
        	"verify" : 1,
        	"dr" : 1
        },
        contentType : 'application/json;charset=UTF-8',
        success: function (data) {
        	if(data.returnCode == "0000") {
        		var option = "<option name='rleId' value=''>请选择认证方式</option>";
        		var length = data.returnObject.length;
        		for(var i = 0;i < length;i++) {
        			option += "<option name='rleId' value='" + data.returnObject[i].rleId + "'>" + data.returnObject[i].rleName + "</option>";
        		}
        		$("#roles").append(option);
        	} else {
        		layer.alert(data.returnMessage);
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error:(");
        }
    });
	//搜索
	$("#searchVerify").click(function(){
		var rleId = $("option[name='rleId']:selected").val();
		var state = $("#state").val();
		var where = $("#where").val();
		$('#verifyTable').dataTable().api().columns([1]).search(rleId);
		$('#verifyTable').dataTable().api().columns([4]).search(state);
		$('#verifyTable').dataTable().api().columns([3]).search(where);
		$('#verifyTable').dataTable().api().draw();
	});
});

/*认证-添加-编辑*/
function verify_add_edit(title,url,w,h){
	layer_show(title,url,w,h);
}

function verify_del(obj,uid,rleId){
	var dataNum = parseInt($("#dataNum").html());
	layer.confirm('认证删除须谨慎，确认要删除吗？',function(index){
		if(uid != "" && rleId != "") {
			$.ajax({
				url: rootPath + '/verify/uid/' + uid + '/rleId/' + rleId,
				type: 'DELETE',
				success: function(data){
					if(data.returnCode == "0000") {						
						layer.msg('已删除!',{icon:1,time:1000},function(){
							$('#refresh').click();
						});
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error:function(data) {
					layer.alert("error:(");
				},
			});		
		}
	});
}


function formatUndefined(value) {
	if(value == undefined) {
		value = "";
	}
	return value;
}

function formatState(state) {
	var value = "";
	switch(state) {
		case 1:
			value = "认证中";
			break;
		case 2:
			value = "认证成功";
			break;
		case 3:
			value = "认证失败";
			break;
		case 4:
			value = "认证过期";
			break;
	}
	return value;
}

function formatInfo(info) {
	return info == undefined ? "" : info;
}

function getExcel(){
	var rleId = $("option[name='rleId']:selected").val();
	var state = $("#state").val();
	var where = $("#where").val();
	
	if(!rleId || rleId =='' ){
		layer.msg("认证方式必选",{icon:2,time:1200})
		return;
	}
	if(!state || state ==''){
		state = 'n';
	}
	if(!where || where ==''){
		where = 'n';
	}
	window.open("verify/downloadExcel/"+rleId+"/"+state+"/"+where);
}